/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cn.lotary.base.validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 *
 * @author user192
 */
public class PatternValidator implements ConstraintValidator<com.cn.lotary.base.validator.Pattern, String> {

    private static String regexp;
    private static boolean unllable;

    @Override
    public void initialize(com.cn.lotary.base.validator.Pattern validator) {
        regexp = validator.regexp();
        unllable = validator.nullable();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (unllable && (value == null || "".equals(value))) {
            return true;
        }
        //TODO:try
        Pattern pattern = Pattern.compile(regexp);
        Matcher matcher = pattern.matcher(value);
        return matcher.matches();
    }
}
